/**
 * 对原生的一些扩展
 */
import _ from 'lodash'
/* eslint-disable no-extend-native */
export default{
  install () {
    // Array
    Array.prototype.indexOf = Array.prototype.indexOf || function (arg) {
      return _.indexOf(this, arg)
    }
    Array.prototype.includes = Array.prototype.includes || function (arg) {
      return _.includes(this, arg)
    }

    // arg是函数
    Array.prototype.filter = Array.prototype.filter || function (func) {
      return _.filter(this, func)
    }
    Array.prototype.find = Array.prototype.find || function (func) {
      return _.find(this, func)
    }
    Array.prototype.findIndex = Array.prototype.findIndex || function (func) {
      return _.findIndex(this, func)
    }

    // String
    String.prototype.cut = function (length) {
      if (this.length < length) return this
      return this.substr(0, length) + '...'
    }

    // 对象
    Object.entries = Object.entries || function (obj) {
      if (!_.isObject(obj)) throw new Error('expect a pure object !')
      let res = []
      for (let k in obj) {
        if (obj.hasOwnProperty(k)) {
          res.push([k, obj[k]])
        }
      }
      return res
    }
  }
}
